//
//  SWPayHistoryViewController.m
//  sc-cyan-ios
//
//  Created by lvjian01 on 2017/12/25.
//  Copyright © 2017年 greenbuilding. All rights reserved.
//

#import "SWPayHistoryViewController.h"
#import "SWPayHistoryTableViewCell.h"

@interface SWPayHistoryViewController ()<SWSegmentedControlDelegate,UITableViewDelegate,UITableViewDataSource,UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *controlContentView;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (nonatomic,strong) SWSegmentedControl *control;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scrollViewContentWidthConstraint;
@property (weak, nonatomic) IBOutlet UIView *scrollContentView;
@property (nonatomic, strong) NSArray *controlDataArray;



@end

@implementation SWPayHistoryViewController
- (NSArray *)controlDataArray{
    return @[@"租赁信息",@"求租信息"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeSubViews];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)currentPage{
    NSInteger page = floor((self.scrollView.contentOffset.x - self.scrollView.width / 2) / self.scrollView.width) + 1;
    //    NSInteger page = round(self.scrollView.contentOffset.x / self.scrollView.width);
    
    if (page < 0) {
        page = 0;
    }else if (page >= self.controlDataArray.count) {
        page = self.controlDataArray.count - 1;
    }
    
    return page;
}

- (void)makeSubViews{
    
    self.control = [SWSegmentedControl segmentedControlFrame:self.controlContentView.bounds titleDataSource:self.controlDataArray backgroundColor:UIColorFromRGB(0xF5F5F5) titleColor:UIColorFromRGB(0x999999) titleFont:SYSTEMFONT(14) selectColor:UIColorFromRGB(0x4D4D4D) bottomColor:UIColorFromRGB(0x4D4D4D) delegate:self];
    [self.controlContentView addSubview:self.control];
    [self.control mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.controlContentView);
    }];
    
    
    CGFloat collectionViewWidth = Main_Screen_Width;
    self.scrollViewContentWidthConstraint.constant = collectionViewWidth * self.controlDataArray.count;
    
    CGFloat x_offset = 0;
    for (int i = 0; i < self.controlDataArray.count; i++) {
        CGRect frame = CGRectMake(x_offset, 0, collectionViewWidth, 100);
        
        UITableView *tableView = [[UITableView alloc] initWithFrame:frame style:UITableViewStylePlain];
        tableView.tag = 100 + i;
        tableView.dataSource = self;
        tableView.delegate = self;
        tableView.alwaysBounceHorizontal = NO;
        tableView.alwaysBounceVertical = YES;
        //        tableView.backgroundColor = UIColorFromRGB(0xF5F5F5);
        tableView.showsHorizontalScrollIndicator = NO;
        tableView.showsVerticalScrollIndicator = YES;
        [self.scrollContentView addSubview:tableView];
        
        //        tableView.estimatedRowHeight = 100;  //  随便设个不那么离谱的值
        //        tableView.rowHeight = UITableViewAutomaticDimension;
        
        
        [tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.equalTo(self.scrollContentView);
            make.width.mas_equalTo(collectionViewWidth);
            if (i == 0) {
                make.leading.equalTo(self.scrollContentView);
            }else{
                make.leading.equalTo([self.scrollContentView viewWithTag:100+i-1].mas_trailing);
            }
            
            if (i == self.controlDataArray.count-1) {
                make.right.equalTo(self.scrollContentView);
            }
        }];
        
        
        tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
        tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        tableView.separatorColor = UIColorFromRGB(0xD4D4D4);
        
        [tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SWPayHistoryTableViewCell class]) bundle:[NSBundle mainBundle]] forCellReuseIdentifier:NSStringFromClass([SWPayHistoryTableViewCell class])];
        
        
        x_offset += collectionViewWidth;
        [tableView reloadData];
    }
    [self.scrollContentView layoutIfNeeded];
    
}

#pragma mark -- SWSegmentedControlDelegate
- (void)segumentSelectionChange:(NSInteger)selection{
    [self.scrollView setContentOffset:CGPointMake(selection * Main_Screen_Width, 0) animated:YES];
}

#pragma mark -- UIScrollViewDelegate
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if (scrollView.tag != 1000) {
        //        只有滑动背景scrollview才能出发
        return;
    }
    
    [self.control selectSegementAtIndex:[self currentPage]];
}

#pragma mark -- UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"......");
}

#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    SWPayHistoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([SWPayHistoryTableViewCell class]) forIndexPath:indexPath];
    //
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 94;
}


@end
